﻿using System.Text;

namespace TPXSDK.NET.Mission.Instruction;

internal class InstWaitSpin : Tokenizer
{
    public InstWaitSpin(TokenizerAdmin admin)
    {
        Admin = admin;
        KeyName = "waitspin";
        KeyValue = (byte)FlyCmdId.CMD_WAITSPIN;
        Usage = "waitspin yaw_angle";
        Description = @"Waits until reached the yaw specified in the yaw_angle parameter.";
    }

    public override bool Process()
    {
        var flyCmp = Admin?.Compiler; if (null == flyCmp) return false;
        var tokens = flyCmp.GetLineTokens();

        if (tokens is not { Count: 2 })
        {
            flyCmp.ErrorMsg = $"{KeyName} Invalid Usage";
            return false;
        }

        var cmd = new FlyCmdItem
        {
            Id = (byte)KeyValue,
            LineNum = tokens[0].Row
        };
        tokens.RemoveAt(0);// Remove Flycmd Name
        if (tokens[0].Type == TokenType.NUMBER)
        {
            cmd.P2 = FlyUtils.ToInt16(tokens[0].Name);
            flyCmp.CmdList.Add(cmd);
            return true;
        }
        flyCmp.ErrorMsg = $"{KeyName} Invalid param";
        return false;
    }

    public override void Reverse(FlyCmdItem cmd, StringBuilder sb)
    {
        sb.AppendLine($"{KeyName} {cmd.P2}//deg");
    }
}
